#!/usr/bin/env python3
#
# Copyright (c) 2010 - 2025, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# We kindly request you to use one or more of the following phrases to refer to
# foxBMS in your hardware, software, documentation or advertising materials:
#
# - "This product uses parts of foxBMS®"
# - "This product includes parts of foxBMS®"
# - "This product is derived from foxBMS®"

"""Helper Build Script: ``./src/app/driver/wscript``
=============================================

Helper script to recursively build the embedded sources"""

import os


def build(bld):
    """builds the driver library"""
    ts_man = bld.env.temperature_sensor_manuf
    ts_mod = bld.env.temperature_sensor_model
    ts_met = bld.env.temperature_sensor_meth
    # fmt: off
    # pylint: disable=line-too-long
    source = [
        os.path.join("adc", "adc.c"),
        os.path.join("can", "can.c"),
        os.path.join("can", "cbs", "can_helper.c"),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_crash-dump.c"),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_debug-build-configuration.c"),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_debug-response.c"),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_debug-unsupported-multiplexer-values.c",),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_imd-request.c"),
        os.path.join("can", "cbs", "tx-async", "can_cbs_tx_fatal-error.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_aerosol-sensor.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_cell-temperatures.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_cell-voltages.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_current-sensor.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_imd-response.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_imd-info.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_debug.c"),
        os.path.join("can", "cbs", "rx", "can_cbs_rx_bms-state-request.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_cell-temperatures.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_cell-voltages.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_pack-limits.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_pack-minimum-maximum-values.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_string-minimum-maximum-values.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_pack-state-estimation.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_bms-state.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_bms-state-details.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_string-state.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_string-state-estimation.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_pack-values-p0.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_pack-values-p1.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_string-values-p0.c"),
        os.path.join("can", "cbs", "tx-cyclic", "can_cbs_tx_string-values-p1.c"),
        os.path.join("checksum", "checksum.c"),
        os.path.join("crc", "crc.c"),
        os.path.join("config", "contactor_cfg.c"),
        os.path.join("config", "can_cfg_tx_cyclic.c"),
        os.path.join("config", "can_cfg_rx.c"),
        os.path.join("config", "can_cfg.c"),
        os.path.join("config", "dma_cfg.c"),
        os.path.join("config", "fram_cfg.c"),
        os.path.join("config", "pex_cfg.c"),
        os.path.join("config", "spi_cfg.c"),
        os.path.join("config", "sps_cfg.c"),
        os.path.join("contactor", "contactor.c"),
        os.path.join("dma", "dma.c"),
        os.path.join("foxmath", "foxmath.c"),
        os.path.join("foxmath", "utils.c"),
        os.path.join("fram", "fram.c"),
        os.path.join("htsensor", "htsensor.c"),
        os.path.join("interlock", "interlock.c"),
        os.path.join("i2c", "i2c.c"),
        os.path.join("io", "io.c"),
        os.path.join("led", "led.c"),
        os.path.join("mcu", "mcu.c"),
        os.path.join("meas", "meas.c"),
        os.path.join("pex", "pex.c"),
        os.path.join("pwm", "pwm.c"),
        os.path.join("rtc", "rtc.c"),
        os.path.join("sbc", "nxpfs85xx.c"),
        os.path.join("sbc", "sbc.c"),
        os.path.join("sbc", "fs8x_driver", "sbc_fs8x_communication.c"),
        os.path.join("sbc", "fs8x_driver", "sbc_fs8x.c"),
        os.path.join("spi", "spi.c"),
        os.path.join("sps", "sps.c"),
        os.path.join("ts", ts_man, ts_mod, ts_met, f"{ts_man}_{ts_mod}" f"_{ts_met}.c"),
        os.path.join("ts", "api", "tsi_limits.c"),
        os.path.join("ts", "epcos", "b57251v5103j060", "epcos_b57251v5103j060.c"),
        os.path.join("ts", "epcos", "b57861s0103f045", "epcos_b57861s0103f045.c"),
        os.path.join("ts", "fake", "none", "fake_none.c"),
        os.path.join("ts", "murata", "ncxxxxh103", "murata_ncxxxxh103.c"),
        os.path.join("ts", "semitec", "103jt", "semitec_103jt.c"),
        os.path.join("ts", "tdk", "ntcgs103jf103ft8", "tdk_ntcgs103jf103ft8.c"),
        os.path.join("ts","tdk","ntcg163jx103dt1s","tdk_ntcg163jx103dt1s.c" ),
        os.path.join("ts", "vishay", "ntcalug01a103g", "vishay_ntcalug01a103g.c"),
        os.path.join("ts", "vishay", "ntcle317e4103sba", "vishay_ntcle317e4103sba.c"),
        os.path.join("ts", "vishay", "ntcle413e2103f102l", "vishay_ntcle413e2103f102l.c"),
        os.path.join("ts", "beta.c"),
    ]
    # pylint: enable=line-too-long
    # fmt: on
    includes = [
        "adc",
        "can",
        "config",
        "checksum",
        "contactor",
        "crc",
        "dma",
        "foxmath",
        "fram",
        "htsensor",
        "interlock",
        "i2c",
        "io",
        "meas",
        "mcu",
        "pex",
        "pwm",
        "rtc",
        "sbc",
        os.path.join("sbc", "fs8x_driver"),
        "spi",
        "sps",
        "ts",
        os.path.join("can", "cbs"),
        os.path.join("can", "cbs", "rx"),
        os.path.join("can", "cbs", "tx-async"),
        os.path.join("can", "cbs", "tx-cyclic"),
        os.path.join("imd"),
        os.path.join("imd", "bender", "iso165c"),
        os.path.join("imd", "bender", "iso165c", "config"),
        os.path.join("ts", "api"),
        os.path.join("ts", "epcos", "b57251v5103j060"),
        os.path.join("ts", "epcos", "b57861s0103f045"),
        os.path.join("ts", "fake", "none"),
        os.path.join("ts", "murata", "ncxxxxh103"),
        os.path.join("ts", "semitec", "103jt"),
        os.path.join("ts", "tdk", "ntcgs103jf103ft8"),
        os.path.join("ts", "tdk", "ntcg163jx103dt1s"),
        os.path.join("ts", "vishay", "ntcalug01a103g"),
        os.path.join("ts", "vishay", "ntcle317e4103sba"),
        os.path.join("ts", "vishay", "ntcle413e2103f102l"),
        os.path.join("..", "application", "bal"),
        os.path.join("..", "application", "bms"),
        os.path.join("..", "application", "config"),
        os.path.join("..", "application", "soa"),
        os.path.join("..", "engine", "config"),
        os.path.join("..", "engine", "database"),
        os.path.join("..", "engine", "diag"),
        os.path.join("..", "engine", "hw_info"),
        os.path.join("..", "engine", "sys"),
        os.path.join("..", "engine", "sys_mon"),
        os.path.join("..", "main", "include"),
        os.path.join("..", "main"),
        os.path.join("..", "task", "config"),
        os.path.join("..", "task", "os"),
        os.path.join("..", "task", "ftask"),
        os.path.join("..", "..", "version"),
    ]
    includes.extend(bld.env.INCLUDES_RTOS + bld.env.INCLUDES_AFE + bld.env.INCLUDES_IMD)
    cflags = bld.env.CFLAGS_FOXBMS
    target = f"{bld.env.APPNAME.lower()}-driver"
    use = [f"{bld.env.APPNAME.lower()}-afe-driver", f"{bld.env.APPNAME.lower()}-imd"]
    bld.stlib(
        source=source,
        includes=includes,
        cflags=cflags,
        use=use,
        target=target,
    )

    bld.recurse("afe")
    bld.recurse("imd")
